package com.partner.match.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.partner.match.common.BaseResponse;
import com.partner.match.common.ErrorCode;
import com.partner.match.common.ResultUtils;
import com.partner.match.exception.BusinessException;
import com.partner.match.model.dto.team.*;
import com.partner.match.model.entity.Team;
import com.partner.match.service.TeamService;
import com.partner.match.service.UserTeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: jzz
 * @CreateTime: 2025-01-25
 * @Description: 队伍控制器
 */
@RestController
@RequestMapping("/team")
@Api(tags = "队伍控制器")
public class TeamController {
    @Resource
    private TeamService teamService;

    @Resource
    private UserTeamService userTeamService;

    @PostMapping("/add")
    @ApiOperation("创建队伍")
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<CreatedTeamResp> addTeam(@RequestBody CreatedTeamReq teamReq){
        // 使用sa-token代替
        long userId = StpUtil.getLoginIdAsLong();
        // 创建队伍
        Long teamId = teamService.addTeam(teamReq,userId);
        // 更新队伍信息表信息
        Long userTeamId = userTeamService.createRelation(teamId,userId);

        // 返回队伍ID和关系ID
        CreatedTeamResp createdTeamResp = new CreatedTeamResp();
        createdTeamResp.setTeamId(teamId);
        createdTeamResp.setUserTeamId(userTeamId);
        return ResultUtils.success(createdTeamResp);
    }

    @PostMapping("/join")
    @ApiOperation("加入队伍")
    public BaseResponse<Long> joinTeam(@RequestBody JoinTeamReq teamReq){
        if(ObjectUtil.isEmpty(teamReq.getTeamId())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long userId = StpUtil.getLoginIdAsLong();
        Long userTeamId = teamService.joinTeam(teamReq,userId);
        return ResultUtils.success(userTeamId);
    }

    @DeleteMapping("/exit/{teamId}")
    @ApiOperation("退出队伍")
    public BaseResponse<Boolean> exitTeam(@PathVariable Long teamId){
        userTeamService.exitTeam(teamId);
        return ResultUtils.success(true);
    }

    @DeleteMapping("/dissolve/{teamId}")
    @ApiOperation("解散队伍")
    public BaseResponse<Boolean> dissolve(@PathVariable Long teamId){
        teamService.dissolveTeam(teamId);
        return ResultUtils.success(true);
    }

    @PostMapping("/update")
    @ApiOperation("修改队伍信息")
    public BaseResponse<Boolean> updateTeam(@RequestBody UpdateTeamReq updateTeamReq){
        if(ObjectUtil.isNull(updateTeamReq)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        teamService.updateTeam(updateTeamReq);
        return ResultUtils.success(true);
    }

    @PostMapping("/selectTeams")
    @ApiOperation("查询队伍信息")
    public BaseResponse<Page<QueryTeamResp>> selectTeams(@RequestBody QueryTeamReq queryTeamReq){
        Long currentPage = queryTeamReq.getCurrentPage();
        Long pageSize = queryTeamReq.getPageSize();
        String teamName = queryTeamReq.getTeamName();

        Page<QueryTeamResp> teamRespPage = teamService.selectTeams(currentPage,pageSize,teamName);

        return ResultUtils.success(teamRespPage);
    }

    @GetMapping("/selectTeam/{teamId}")
    @ApiOperation("根据Id查询队伍信息")
    public BaseResponse<UpdateTeamReq> selectTeam(@PathVariable Long teamId){
        if(ObjectUtil.isEmpty(teamId)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = teamService.getById(teamId);
        UpdateTeamReq updateTeamReq = BeanUtil.copyProperties(team, UpdateTeamReq.class);
        return ResultUtils.success(updateTeamReq);
    }

    @PostMapping("/selectTeam/self")
    @ApiOperation("查看自己创建的队伍")
    public BaseResponse<Page<QueryTeamResp>> selectTeamSelf(@RequestBody QueryTeamReq queryTeamReq){
        long currentPage = queryTeamReq.getCurrentPage();
        long pageSize = queryTeamReq.getPageSize();
        Page<QueryTeamResp> selfTeamPage = teamService.selectTeamSelf(currentPage,pageSize);
        return ResultUtils.success(selfTeamPage);
    }

    @PostMapping("/selectTeam/join/self")
    @ApiOperation("查看自己加入的队伍信息")
    public BaseResponse<Page<QueryTeamResp>> selectJoinTeamSelf(@RequestBody QueryTeamReq queryTeamReq){
        long currentPage = queryTeamReq.getCurrentPage();
        long pageSize = queryTeamReq.getPageSize();
        Page<QueryTeamResp> joinTeamSelf = teamService.selectJoinTeamSelf(currentPage,pageSize);
        return ResultUtils.success(joinTeamSelf);
    }
}
